<template>
  <div class="app-container">
    <div class="search">
      <!-- 搜索表单 -->
      <el-form :model="serchForm" ref="formRef">
        <el-row :gutter="20">
          <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="单位名称" prop="account">
              <el-input
                v-model="serchForm.model.corporateName"
                placeholder="请输入单位名称"
                style="width: 200px"
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="申请人" prop="account">
              <el-input
                v-model="serchForm.model.applicantName"
                placeholder="请输入申请人"
                style="width: 200px"
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="8">
            <el-form-item label="是否联系">
              <el-select
                v-model="serchForm.model.contactStatus"
                placeholder="请选择"
                size="mini"
              >
                <el-option label="是" :value="1"></el-option>
                <el-option label="否" :value="0"></el-option>
                <!-- <el-option
                  v-for="item in statusOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option> -->
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8"
            ><div class="grid-content ep-bg-purple" />
            <el-form-item label="创建时间" prop="createAt">
              <el-date-picker
                v-model="serchForm.createAt"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间" /></el-form-item
          ></el-col> -->
        </el-row>
      </el-form>
      <!--重置 搜搜按钮 -->
      <div class="btns">
        <el-button icon="Refresh" @click="handleResetClick">重置</el-button>
        <el-button type="primary" icon="Search" @click="handleQueryClick"
          >查询</el-button
        >
      </div>
    </div>

    <!-- 表单 -->
    <div class="content">
      <div class="content">
        <div class="table">
          <el-table :data="dataList" border style="width: 100%">
            <el-table-column
              align="center"
              type="index"
              label="序号"
              width="80px"
            />
            <el-table-column
              align="center"
              prop="corporateName"
              label="单位名称 "
              width="150"
            />

            <el-table-column
              align="center"
              prop="applicantName"
              label="申请人"
              width="120"
            />
            <el-table-column
              align="center"
              prop="email"
              label="邮箱"
              width="120"
            />
            <el-table-column
              align="center"
              prop="phone"
              label="电话"
              width="180"
            />
            <el-table-column align="center" prop="appName" label="软件名称" />
            <el-table-column
              align="center"
              prop="requirement"
              label="需求描述"
              show-overflow-tooltip
            />
            <el-table-column
              align="center"
              prop="createTime"
              label="申请时间"
              width="200"
            >
              <template #default="scope">
                {{ formatDateTime(scope.row.createTime) }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="contactStatus"
              label="联系状态"
              width="100"
            >
              <template #default="scope">
                <el-switch
                  v-model="scope.row.contactStatus"
                  :active-value="1"
                  :inactive-value="0"
                  @change="changeShow(scope.row)"
                />
              </template>
            </el-table-column>

            <el-table-column
              align="center"
              prop="contactStatus"
              label="操作"
              width="100"
            >
              <template #default="scope">
                <el-button type="primary" @click="handleUpdate(scope.row)"
                  >授权</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="pagination">
          <el-pagination
            v-model:current-page="serchForm.current"
            v-model:page-size="serchForm.size"
            :page-sizes="[10, 20, 30]"
            layout="->,total,sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref, onMounted } from "vue";
import { formatDateTime } from "@/utils/index.js";
import {
  queryInfoListRequest,
  updateInfoStatusRequest,
} from "@/api/buildlibrary/information.js";
import { ElMessage } from "element-plus";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();

const total = ref(0);

const serchForm = reactive({
  current: 1,
  size: 10,
  model: {
    corporateName: "",
    applicantName: "",
    contactStatus: "",
  },
});

onMounted(() => {
  getList();
});

// 列表数据
const dataList = ref([]);
async function getList() {
  const res = await queryInfoListRequest(serchForm);
  console.log(res);
  dataList.value = res.data.records;
  total.value = res.data.total;
}

//重置
function handleResetClick() {
  console.log("reset");
  serchForm.current = 1;
  serchForm.size = 10;
  serchForm.model = {
    corporateName: "",
    applicantName: "",
    contactStatus: "",
  };
  getList();
}
//查询操作
function handleQueryClick() {
  serchForm.current = 1;
  console.log("handleQueryClick", serchForm);
  getList();
}

//分页器size事件
const handleSizeChange = (size) => {
  serchForm.size = size;
  getList();
};
//分页器current事件
const handleCurrentChange = (current) => {
  serchForm.current = current;
  getList();
};

//状态改版事件
async function changeShow(row) {
  const { contactStatus, id } = row;
  const data = {
    id,
    contactStatus,
  };
  const res = await updateInfoStatusRequest(data);
  console.log(res);
  if (res.code === 200) {
    ElMessage.success("状态修改成功！");
    getList();
  }
}

//授权按钮操作
const handleUpdate = (row) => {
  console.log(row);
  router.push({
    path: "/appstore/dealrecord",
    query: {
      appName: row.appName,
      appId: row.appId,
    },
  });
};
</script>
<style scoped>
label {
  font-weight: 0 !important;
}
.search {
  background-color: #fff;
  padding: 20px;
}
.search .btns {
  text-align: right;
  padding: 0 50px 10px 0;
}
.pagination {
  margin-top: 14px;
}
</style>
